﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class AdminForm : System.Windows.Forms.Form
    {
        private MainForm form;
        private int projID;

        public AdminForm(MainForm mainForm, int projectID)
        {
            InitializeComponent();
            form = mainForm;
            projID = projectID;

            if (mainForm.GetLogMenuItem() == CheckState.Unchecked)
            {
                customTabControl1.Controls.Remove(tabLog);
            }
            else
            {
                LoadLogView();
            }

            // Add Member List
            CCL.SocketMessage.Request.GetProjectMemberList packet = new CCL.SocketMessage.Request.GetProjectMemberList(projectID);
            CCL.SocketMessage.Message message = CCL.SocketMessage.Message.MessageRequestBuilder(packet);
            byte[] msg = message.GetByte();

            CCL.SocketMessage.Message memMsg = MainForm.connector.Execute(msg);
            CCL.SocketMessage.Response.GetProjectMemberList memResponse = (CCL.SocketMessage.Response.GetProjectMemberList)memMsg.Body;

            if (memResponse.Result == CCL.SocketMessage.Constant.Result.Success)
            {
                List<CCL.TableStructure.ProjectMember> memList = memResponse.ProjectMemberList;

                for (int i = 0; i < memList.Count; i++)
                {
                    memberList.Items.Add(CCL.Utility.StringUtility.ConvertToStr(memList[i].UserInfo.Email) + " (" + CCL.Utility.StringUtility.ConvertToStr(memList[i].UserInfo.Name) + ")");
                }
            }

            lbProjName.Text += CCL.Utility.StringUtility.ConvertToStr(MainForm.GetProjectInfo(projID).ProjectName);
        }

        public void LoadLogView()
        {
            CCL.SocketMessage.Request.ShowLog packet = new CCL.SocketMessage.Request.ShowLog(projID);
            CCL.SocketMessage.Message message = CCL.SocketMessage.Message.MessageRequestBuilder(packet);
            byte[] msg = message.GetByte();

            CCL.SocketMessage.Message logMsg = MainForm.connector.Execute(msg);
            CCL.SocketMessage.Response.ShowLog logResponse = (CCL.SocketMessage.Response.ShowLog)logMsg.Body;

            if (logResponse.Result == CCL.SocketMessage.Constant.Result.Success)
            {
                List<CCL.TableStructure.ProjectRevision> revisionList = logResponse.RevisionDataList;
                for (int i = 0; i < revisionList.Count; i++)
                {
                    string[] values = {CCL.Utility.StringUtility.ConvertToStr(revisionList[0].UserInfo.Email), CCL.Utility.StringUtility.ConvertToStr(revisionList[0].Log), CCL.Utility.StringUtility.ConvertToStr(revisionList[0].RevisionedDate) };
                    ListViewItem item = new ListViewItem(values);
                    logView.Items.Add(item);
                }
            }
            else
            {
                MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(logResponse.ErrorMessage));
            }
        }

        // 탭 컨트롤 닫을때 발생하는 이벤트
        // tablIndex == 0 인 경우 닫지 않음.
        private void customTabControl1_TabClosing(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPageIndex == 0)
            {
                e.Cancel = true;
            }
            if (e.TabPage == tabLog)
            {
                customTabControl1.Controls.Remove(tabLog);
                form.ChangeLogMenuItem();
                e.Cancel = true;
            }
        }

        // 취소 버튼을 눌렀을때
        // 프로그램 걍 꺼짐.
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (memberList.Items.Count != 0)
            {
                // 멤버 리스트에 추가된 내용이 있을 경우
                List<CCL.SocketMessage.Request.RegistProjectMemberList.Member> memList = new List<CCL.SocketMessage.Request.RegistProjectMemberList.Member>();

                System.Collections.IEnumerator ie = memberList.Items.GetEnumerator();
                while(ie.MoveNext())
                {
                    CCL.SocketMessage.Request.RegistProjectMemberList.Member mem = new CCL.SocketMessage.Request.RegistProjectMemberList.Member((string)ie.Current, CCL.SocketMessage.Constant.Permission.All);
                    memList.Add(mem);
                }
                
                CCL.SocketMessage.Request.RegistProjectMemberList packet = new CCL.SocketMessage.Request.RegistProjectMemberList(projID, memList);
                CCL.SocketMessage.Message message = CCL.SocketMessage.Message.MessageRequestBuilder(packet);
                byte[] msg = message.GetByte();

                CCL.SocketMessage.Message memberMsg = MainForm.connector.Execute(msg);
                CCL.SocketMessage.Response.RegistProjectMemberList memResponse = (CCL.SocketMessage.Response.RegistProjectMemberList)memberMsg.Body;
                if (memResponse.Result == CCL.SocketMessage.Constant.Result.Success)
                {
                    this.Close();
                }
                else
                {
                    MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(memResponse.ErrorMessage));
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text != "")
            {
                // Email 주소를 입력하였는지 확인
                if (txtUserName.Text.Contains('@') == true)
                {
                    // Server 에 Email 주소를 송신하여 회원이 존재하는지 확인.


                    // 멤버리스트에 추가
                    memberList.Items.Add(txtUserName.Text);
                }
                else
                {
                    MessageBox.Show("이메일 주소가 아닙니다.");
                }

            }
            else
            {
                // txtUserName 에 데이터가 들어있지 않을 경우
                MessageBox.Show("이메일 주소를 입력하세요.");
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            // Check 한 목록들을 삭제하는 경우
            CheckedListBox.CheckedItemCollection cc = memberList.CheckedItems;
            foreach (object item in cc)
            {
                memberList.Items.Remove(item);
                break;
            }

            // Select 한 목록을 삭제하는 경우
            ListBox.SelectedIndexCollection sc = memberList.SelectedIndices;
            foreach (int index in sc)
            {
                memberList.Items.Remove(memberList.Items[index]);
            }
        }
    }
}
